<template>
    <div>
      <div class="mui-content">
        <form class="mui-input-group">
          <div class="mui-input-row">
            <label>旧密码</label>
            <input v-model="passwordForm.oldPassword" type="password" class="mui-input-clear mui-input" placeholder="请输入旧密码" />
          </div>
          <div class="mui-input-row">
            <label>新密码</label>
            <input v-model="passwordForm.newPassword" type="password" class="mui-input-clear mui-input" placeholder="请输入新密码" />
          </div>
          <div class="mui-input-row">
            <label>确认</label>
            <input type="password" ref="newPwdConfirm" class="mui-input-clear mui-input" placeholder="请确认新密码" />
          </div>
        </form>
        <div class="mui-content-padded">
          <button @click="changePassword" class="mui-btn mui-btn-block mui-btn-primary">修改密码</button>
        </div>
        <div class="mui-content-padded">
          <p>密码修改后，请使用新密码登录</p>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  export default {
    data() {
      return {
        passwordForm: {
          oldPassword: '',
          newPassword: ''
        }
      }
    },
    methods: {
      changePassword: function () {
        var newPwdConfirm = this.$refs.newPwdConfirm.value
        if (this.passwordForm.oldPassword === '') {
          this.$toast('旧密码不能为空')
          return
        } else if (this.passwordForm.newPassword === '') {
          this.$toast('新密码不能为空')
          return
        } else if (this.passwordForm.newPassword != newPwdConfirm) {
          this.$toast('新密码两次输入不一致')
          return
        }
        this.$indicator.open({
          text: '修改中'
        })
        this.$http.put(`user/${this.$store.state.user.id}/password`, this.passwordForm).then(res => {
          this.$indicator.close()
          if (res.data.code === 0) {
            this.$toast(res.data.msg)
          } else {
            this.$router.replace({ name: 'user' })
            this.$toast(res.data.msg)
          }
        }).catch(() => {
          this.$toast('修改密码失败')
          this.$indicator.close()
        })
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .mui-input-group {
    margin-top: 10px;
    background-color: transparent;
  }
  
  .mui-input-group label {
    width: 22%;
  }
  
  .mui-input-row:last-child {
    background: #fff;
  }
  
  .mui-input-row {
    margin-top: 20px;
    background: #fff;
  }
  
  .mui-input-row label~input,
  .mui-input-row label~select,
  .mui-input-row label~textarea {
    width: 78%;
  }
  
  .link-area {
    display: block;
    margin-top: 25px;
    text-align: center;
  }
  
  .mui-content-padded {
    margin-top: 30px;
  }
  </style>